import { Component } from '@angular/core';
import {App, IonicPage, NavController, NavParams} from 'ionic-angular';
import {InfoProvider} from "../../providers/InfoProvider";
import {HttpService} from "../../providers/HttpService";
import {CommProvider} from "../../providers/CommProvider";
import {NativeProvider} from "../../providers/NativeProvider";

/**
 * Generated class for the SettingPage page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */
@IonicPage()
@Component({
  selector: 'page-setting',
  templateUrl: 'setting.html',
})
export class SettingPage {

    setting_items: Array<{title: string, content: string, value: number, checked: boolean, type: number}> = [
        {
            title: '设备信息',
            content: null,
            value: 0,
            checked: false,
            type: 1
        },
        {
            title: '软件版本',
            content: null,
            value: 1,
            checked: false,
            type: 1
        },
        {
            title: '实时数据',
            content: null,
            value: 2,
            checked: true,
            type: 2
        },
        {
            title: '用户退出',
            content: null,
            value: 10,
            checked: false,
            type: 1
        },
    ];

    version_index: number = 1;
    settings: any = {
        data_update: true
    };

    user_info: any;

    ionViewDidLoad() {
        console.log('ionViewDidLoad SettingPage');
        if (this.infoProvider.isNative()) {
            this.nativeProvider.getAppVersionNumber().then(value => {
                this.setting_items[this.version_index].content = value;
            });
            if (this.user_info.type > 8) {
                this.nativeProvider.getAppVersionCode().then(value => {
                    this.setting_items[this.version_index].content += ` ${value}`;
                });
            }
        }
    }

    constructor(public navCtrl: NavController, public navParams: NavParams,
                private appCtrl: App,
                private commProvider: CommProvider,
                private infoProvider: InfoProvider,
                private nativeProvider: NativeProvider,
                private httpService: HttpService) {
        this.user_info = this.infoProvider.getUserInfo();
        if (this.user_info.type < 2) {
            this.setting_items.splice(0, 1);
            this.version_index = 0;
        }
    }

    itemSelected(value) {
        switch (value) {
            case 0:
                this.navCtrl.push('SystemPage');
                break;

            case 1:
                if (this.infoProvider.isAndroid()) {
                    this.nativeProvider.checkAppUpdate().then(data => {
                        this.httpService.instance()
                            .setUrl('/system/reportAppVersion')
                            .setBody(data)
                            .postRequest()
                            .then(res => {
                                console.log('reportAppVersion:', res);
                            });
                        if (data.code == 201) {
                            this.commProvider.showToast('检测到新版本');
                        } else if (data.code == 202) {
                            this.commProvider.showToast('软件已经是最新版本');
                        } else if (data.code == 203) {
                            this.commProvider.showToast('软件正在更新');
                        }
                    });
                }
                break;

            case 10:
                this.userLogout();
                break;
        }
    }

    userLogout() {
        if (this.user_info.type >= 0) {
            this.commProvider.showConfirmAlert('退出登录', '确定要退出登录吗？').then(confirm => {
                if (confirm) {
                    this.httpService.instance()
                        .setUrl('/user/userLogout')
                        .postRequest()
                        .then(res => {
                            console.log('userLogout:', res);
                        });

                    this.appCtrl.getRootNav().setRoot('LoginPage')
                        .then(res => {
                            if (res) {
                                this.infoProvider.clearStorage();
                            }
                        });
                }
            });
        } else {
            this.appCtrl.getRootNav().setRoot('LoginPage')
                .then(res => {
                    if (res) {
                        this.infoProvider.clearStorage();
                    }
                });
        }
    }

    onDataUpdate() {
        console.log('onDataUpdate:', this.settings.data_update);
        /*this.commProvider.showConfirmAlert(this.settings.data_update ? '开启数据自动更新' : '关闭数据自动更新')
            .then(confirm => {
                if (confirm) {

                } else {
                    this.settings.data_update = !this.settings.data_update;
                }
            });*/
    }
}
